package com.personal.common.util;


import java.util.HashMap;
import java.util.Map;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

public class DesEncryption {
	private static final String DESKEY = "general_";
	
	public static void main(String[] args) {
    	Map<String, Object> map = new HashMap<String, Object>();
    	map.put("phone", "15201405559");
    	map.put("barcode", "JPE2lPaRWnYum307");
    	map.put("materialsId", 137);
    	map.put("materialsName", "测试物资");
    	map.put("province", "广东省");
    	map.put("city", "深圳市");
    	map.put("ip", "192.168.1.140");
    	map.put("ip", "192.168.1.140");
    	map.put("timestamp", System.currentTimeMillis());
    	map.put("barcodeCount",158);

    	String text = Json.toJson(map);
	        System.out.println("加密前的内容:" + text);

	        String cryperText = "";
	        try {
	            cryperText = encrypt(text);
	            System.out.println("加密后的内容:" + cryperText);
	        } catch (Exception e) {
	            e.printStackTrace();
	        }

	        try {
	            System.out.println("解密后的内容:" + decrypt(cryperText));
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	}
	
	
	public static String decrypt(String text) throws Exception {
        byte[] bytesrc = convertHexString(text);
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        DESKeySpec desKeySpec = new DESKeySpec(DESKEY.getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        IvParameterSpec iv = new IvParameterSpec(DESKEY.getBytes("UTF-8"));
        cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
        byte[] retByte = cipher.doFinal(bytesrc);
        return new String(retByte, "UTF-8");
    }

    public static String encrypt(String text) throws Exception {
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        DESKeySpec desKeySpec = new DESKeySpec(DESKEY.getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        IvParameterSpec iv = new IvParameterSpec(DESKEY.getBytes("UTF-8"));
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
        return toHexString(cipher.doFinal(text.getBytes("UTF-8")));
    }

    public static byte[] convertHexString(String ss) {
        byte digest[] = new byte[ss.length() / 2];
        for (int i = 0; i < digest.length; i++) {
            String byteString = ss.substring(2 * i, 2 * i + 2);
            int byteValue = Integer.parseInt(byteString, 16);
            digest[i] = (byte) byteValue;
        }
        return digest;
    }

    public static String toHexString(byte b[]) {
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            String plainText = Integer.toHexString(0xff & b[i]);
            if (plainText.length() < 2)
                plainText = "0" + plainText;
            hexString.append(plainText);
        }
        return hexString.toString();
    }

	
}
